package com.atguigu.lease.common.exception;

import com.atguigu.lease.common.result.Result;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.atguigu.lease.common.exception
 * Description:
 *
 * @Autor: xvyy
 * @Create: 2024/11/18 - 12:55
 * @Version: v1.0
 */
//@ControllerAdvice
@RestControllerAdvice // 相当于@ControllerAdvice + @ResponseBody
//@Builder
@Slf4j
@Log4j2
public class GlobalExceptionHandler {

    //todo 全局异常处理
//    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        e.printStackTrace();
//        log.info(Arrays.toString(e.getStackTrace()));
        return Result.fail();
    }

    //todo LeaseException处理
    @ExceptionHandler(LeaseException.class)
    public Result error(LeaseException leaseException) {
        leaseException.printStackTrace();
//        log.info(Arrays.toString(leaseException.getStackTrace()));
        return Result.build(null, leaseException.getCode(), leaseException.getMessage());
    }
}
